package com.crayon2f.test;

import java.util.Objects;

public class SortedNode {

    private SortedNode next;
    private final int value;

    public SortedNode(int value) {
        this.value = value;
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder(value);
        SortedNode temp = this;
        while (Objects.nonNull(temp.next)) {
            builder.append(String.format(" %d ->", temp.next.value));
            temp = temp.next;
        }
        return builder.toString();
    }

    public static SortedNode merge(SortedNode node1, SortedNode node2) {

        if (Objects.isNull(node1)|| Objects.isNull(node2)) {
            return Objects.nonNull(node1) ? node1 : node2;
        }
        SortedNode head = node1.value < node2.value ? node1 : node2;
        SortedNode current1 = head == node1 ? node1 : node2;
        SortedNode current2 = head == node1 ? node2 : node1;
        SortedNode pre = null, next;
        while (Objects.nonNull(current1) && Objects.nonNull(current2)) {
            if (current1.value <= current2.value) {
                pre = current1;
                current1 = current1.next;
            } else {

            }
        }

        return head;

    }

    public static void main(String[] args) {

        SortedNode node1 = new SortedNode(1);
        SortedNode node3 = new SortedNode(3);
        SortedNode node5 = new SortedNode(5);

        node1.next = node3;
        node3.next = node5;

        SortedNode node2 = new SortedNode(2);
        SortedNode node4 = new SortedNode(4);
        SortedNode node6 = new SortedNode(6);

        node2.next = node4;
        node4.next = node6;

        SortedNode merge = merge(node1, node2);
        System.out.println(merge);
    }
}
